#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
交易 - C2B扫码支付预授权功能使用示例
"""

import os
import sys
import uuid

# 添加项目根目录到Python路径，确保能够导入jlpay包
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from examples.base_example import BaseExample
from jlpay.services.trans.c2b.qrcode_pay_pre_auth_service import QrcodePayPreAuthService
from jlpay.model.trans.c2b.qrcode_pay_pre_auth import (
    QrcodePayPreAuthRequest, QrcodePayPreAuthResponse
)
from jlpay.model.trans.c2b.qrcode_pay import TerminalInfoDto


class QrcodePayPreAuthExample(BaseExample):
    """扫码支付预授权示例类"""
    
    def __init__(self):
        """初始化示例类"""
        super().__init__("examples.trans.c2b.qrcode_pay_pre_auth")
        self.qrcode_pay_pre_auth_service = QrcodePayPreAuthService(self.config)
    
    def build_terminal_info(self):
        """
        构建终端信息数据
        
        Returns:
            TerminalInfoDto实例
        """
        terminal_info = TerminalInfoDto()

        return terminal_info
    
    def build_qrcode_pay_pre_auth_request(self):
        """
        构建扫码支付预授权请求
        
        Returns:
            QrcodePayPreAuthRequest实例
        """
        request = QrcodePayPreAuthRequest()
        
        # 设置请求参数
        request.set_mch_id("849584358120018")
        request.set_term_no("60131234")
        request.set_pay_type("wxpay")
        request.set_out_trade_no("ld201911023465529")
        request.set_payment_valid_time("10")
        request.set_body("测试扫码预授权商品")
        request.set_attach("测试扫码预授权描述")
        request.set_total_fee("300")
        request.set_remark("测试扫码预授权备注")
        request.set_notify_url("https://example.com/notify")
        request.set_op_user_id("test_user")
        request.set_op_shop_id("GD_SZ_001")
        request.set_device_info("N50007A8890")
        request.set_mch_create_ip("127.0.0.1")
        request.set_trans_address("北京市朝阳区")
        request.set_area_code("110105")
        request.set_longitude("116.397128")
        request.set_latitude("39.916527")
        request.set_sub_appid("wxid_129752pfasf1671")
        
        # 设置终端信息
        terminal_info = self.build_terminal_info()
        request.set_terminal_info(terminal_info)
        
        return request
    
    def demo(self):
        """
        执行扫码支付预授权示例
        """
        self.logger.info("开始执行扫码支付预授权示例")
        
        # 创建扫码支付预授权请求
        request = self.build_qrcode_pay_pre_auth_request()
        
        # 发送请求
        response = self.send_request_and_handle_response(
            self.qrcode_pay_pre_auth_service.qrcode_pay_pre_auth,
            "扫码支付预授权",
            request
        )
        
        self.logger.info("扫码支付预授权示例执行完毕")


if __name__ == "__main__":
    logger = QrcodePayPreAuthExample().logger
    logger.info("==================================================")
    logger.info("===== 扫码支付预授权示例 开始 =====")
    logger.info("==================================================")
    
    example = QrcodePayPreAuthExample()
    example.demo()
    
    logger.info("==================================================")
    logger.info("===== 扫码支付预授权示例执行完毕 =====")
    logger.info("==================================================") 